import { useQuery } from '@tanstack/react-query';
import { useChain } from '@/context/ChainContext';

export interface NetworkInfo {
  chainId: number;
  name: string;
  rpcUrl: string;
  explorerUrl: string;
  nativeCurrency: {
    name: string;
    symbol: string;
    decimals: number;
  };
  blockTime: number;
  confirmations: number;
}

// Mock network info to avoid API calls
const mockNetworkInfo: Record<string, NetworkInfo> = {
  mainnet: {
    chainId: 56,
    name: 'BSC Mainnet',
    rpcUrl: 'https://bsc-dataseed.binance.org/',
    explorerUrl: 'https://bscscan.com',
    nativeCurrency: {
      name: 'BNB',
      symbol: 'BNB',
      decimals: 18,
    },
    blockTime: 3,
    confirmations: 12,
  },
  testnet: {
    chainId: 97,
    name: 'BSC Testnet',
    rpcUrl: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
    explorerUrl: 'https://testnet.bscscan.com',
    nativeCurrency: {
      name: 'BNB',
      symbol: 'BNB',
      decimals: 18,
    },
    blockTime: 3,
    confirmations: 12,
  },
};

export function useNetworkInfo() {
  const { currentChain } = useChain();

  return useQuery({
    queryKey: ['network-info', currentChain],
    queryFn: async (): Promise<NetworkInfo> => {
      // Return mock data instead of making API call
      return mockNetworkInfo[currentChain] || mockNetworkInfo.mainnet;
    },
    staleTime: 5 * 60 * 1000, // 5 minutes
    retry: false,
  });
}

export function getExplorerUrl(hash: string, type: 'tx' | 'address' = 'tx'): string {
  const { currentChain } = useChain();
  const networkInfo = mockNetworkInfo[currentChain] || mockNetworkInfo.mainnet;
  
  return `${networkInfo.explorerUrl}/${type}/${hash}`;
}

export function getExplorerUrlStatic(hash: string, chain: string, type: 'tx' | 'address' = 'tx'): string {
  const networkInfo = mockNetworkInfo[chain] || mockNetworkInfo.mainnet;
  return `${networkInfo.explorerUrl}/${type}/${hash}`;
}